using UnityEngine;
using System.Collections;
using System;

/// Applovin广告
public class ApplovinAds : AdsBase
{
	
	public override void Init (params string[] key)
	{
		TJLog ("Applovin Init key" + key [0]);
		AppLovin.SetSdkKey (key [0]);
		AppLovin.InitializeSdk ();
		AppLovin.SetUnityAdListener (key [1]);
		LoadIncent ();
		LoadInsert ();
	}
	
	#region  展示banner 视屏 插屏
	
	public override void ShowIncent (Action<bool> ac)
	{
		incentCallBack = ac;

		TJLog ("展示激励视频");
		AppLovin.ShowRewardedInterstitial ();
	}
	
	public override void ShowInsertVideo (Action<bool> ac)
	{
		insertCallBack = ac;

		TJLog ("展示插屏视频");
		AppLovin.ShowInterstitial ();
	}
	
	#endregion
	
	public override void OnADCallback (string ev)
	{
		TJLog ("回调信息: " + ev);
		if (ev.Contains ("REWARDAPPROVEDINFO")) {
			string[] split = ev.Split ('|');
			double amount = double.Parse (split [1]);
			string currencyName = split [2];
			
			TJLog ("视频开始播放  ");
		} else if (ev.Contains ("LOADEDREWARDED")) {
			TJLog ("load reward video success");
		} else if (ev.Contains ("LOADEDINTER")) {
			TJLog ("load inter video success");
		} else if (ev.Contains ("LOADREWARDEDFAILED")) {
			TJLog ("load reward video fail go to load again");
			LoadIncent ();
		} else if (ev.Contains ("LOADINTERFAILED")) {
			TJLog ("load inter video fail go to load again");
			LoadInsert ();
		} else if (ev.Contains ("HIDDENREWARDED") || ev.Contains ("HIDDENINTER")) {
			TJLog ("视频播放完成，现在取下次视频");
			if (incentCallBack != null){
				incentCallBack (true);
				incentCallBack = null;
			}
			LoadIncent ();
		}
	}
	
	#region 获取视频和插屏
	
	public override void LoadIncent ()
	{
		TJLog ("获取视频");
		AppLovin.LoadRewardedInterstitial ();
	}
	
	public override void LoadInsert ()
	{
		TJLog ("获取插频");
		AppLovin.PreloadInterstitial ();
	}
	
	#endregion
	
	#region 判断视频和插屏是否加载好

	public override bool IsBannerReady ()
	{	
		return false;
	}

	public override bool IsIncentReady ()
	{
		
		if (AppLovin.IsIncentInterstitialReady ()) {
			TJLog ("视频加载好啦");
			return true;
		} else {
			TJLog ("视频没加载好");
			LoadIncent ();
		}
		
		return false;
	}
	
	public override bool IsInsertReady ()
	{
		if (AppLovin.HasPreloadedInterstitial ()) {
			TJLog ("插频加载好啦");
			return true;
		} else {
			TJLog ("插频没加载好");
			LoadInsert ();
			return false;
		}
	}
	
	#endregion
	
}
